﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace NG
{
    class DBConnect
    {
        private string server;
        private string database;
        public string dbName;
        private SqlConnection myCon;

        //Constructor
        public DBConnect()
        {
            Initialize();
        }

        //Initialize values
        public void Initialize()
        {
            dbName = "AnalysisDBSQL";
            server = "server = " + ".\\SQLEXPRESS;";
            database = "database = " + dbName + ";" ;
            
            string connectionString;  
            connectionString = server + "Trusted_Connection = yes;" + database + "connection timeout=10" ;
          
            myCon = new SqlConnection(connectionString);


        }



        
        //open connection to database
        public bool OpenConnection()
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (SqlException ex)
            {
                //When handling errors, you can your application's response based 
                //on the error number.
                //The two most common error numbers when connecting are as follows:
                //0: Cannot connect to server.
                //1045: Invalid user name and/or password.
                switch (ex.Number)
                {
                    case 0:
                        MessageBox.Show("Cannot connect to server.  Contact administrator");
                        break;

                    case 1044:
                        MessageBox.Show("Invalid username/password, please try again");
                        break;
                }
                return false;
            }

            //Close connection

        }

        public bool CloseConnection()
        {
            try
            {
                myCon.Close();
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
  
        }
        public SqlConnection connection
        {
            get
            {
            return this.myCon;
          }
        }
    }

        
    
}
   

    